package com.polaris.live.ui.me

import android.os.Bundle
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayoutMediator
import com.polaris.live.R
import com.polaris.live.common.R.color
import com.polaris.live.common.adapter.ViewPagerAdapter
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.util.ImmersionBarBuilder
import com.polaris.live.databinding.ActivityRichCharmBinding
import com.polaris.live.resp.back_resp.UserRichCharmConfigVo
import com.polaris.live.translation.R.string
import com.polaris.live.utils.getIntOrString
import com.polaris.live.viewmodel.me.RichCharmViewModel

/**
 * 壕魅等级
 *
 * @author Created by lucas on 2023/10/16 17:10
 * @since 1.0.0
 */
class RichCharmActivity : BaseVbActivity<RichCharmViewModel, ActivityRichCharmBinding>() {

    private val mMeRichFragment by lazy {
        MeRichCharmFragment().apply {
            arguments = bundleOf("type" to RICH)
            setOnPageChangeCallback { setImageBack(it) }
        }
    }
    private val mMeCharmFragment by lazy {
        MeRichCharmFragment().apply {
            arguments = bundleOf("type" to CHARM)
            setOnPageChangeCallback { setImageBack(it) }
        }
    }
    private val mMeGameFragment by lazy { MeGameFragment() }

    private val mPage: Int by lazy { intent.getIntOrString(PAGE, 0) }

    private val mGameType: Int by lazy { intent.getIntOrString("gameType", -1) }

    override fun ImmersionBarBuilder.buildImmersionBar() {
        setTitleBar(mBinding.llTab)
        setStatusBarDarkFont(false)
        setNavigationBarDarkFont(false)
        attachImmersionBar { transparentBar() }
    }

    override fun initView(savedInstanceState: Bundle?) {
        val adapter = ViewPagerAdapter(
            mutableListOf(mMeRichFragment, mMeCharmFragment, mMeGameFragment),
            this
        )
        mBinding.vp2.adapter = adapter

        val tabText =
            mutableListOf(
                getString(string.me_rich),
                getString(string.me_charm),
                getString(string.game_level)
            )
        TabLayoutMediator(mBinding.tab, mBinding.vp2) { tab, position ->
            val tabView = TextView(this)
            tabView.setBackgroundColor(ContextCompat.getColor(this, android.R.color.transparent))
            tabView.text = tabText[position]
            tabView.textSize = 16F
            tab.customView = tabView
        }.attach()
        mBinding.ivBack.setOnSingleClickListener {
            finish()
        }
        mBinding.vp2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                if (position == 0) {
                    mMeRichFragment.getMaxLevel()
                } else if (position == 1) {
                    mMeCharmFragment.getMaxLevel()
                } else {
                    mBinding.root.setBackgroundResource(R.drawable.back_black)
                }
                for (index in 0 until mBinding.tab.tabCount) {
                    val tabView = mBinding.tab.getTabAt(index)?.customView as TextView
                    if (position == index) {
                        tabView.setTextColor(
                            ContextCompat.getColor(this@RichCharmActivity, R.color.white)
                        )
                    } else {
                        tabView.setTextColor(
                            ContextCompat.getColor(this@RichCharmActivity, color.color_80ffffff)
                        )
                    }
                }
            }
        })
        mBinding.vp2.setCurrentItem(mPage, false)
        //这样写没问题的，因为顺序是写死的
        if (mPage == 2) {
            mMeGameFragment.arguments = bundleOf("gameType" to mGameType)
        }
    }

    private fun setImageBack(userRichCharmConfigVo: UserRichCharmConfigVo) {
        val currentItem = mBinding.vp2.currentItem
        if (currentItem == 0 || currentItem == 1) {
            val maxLevel = userRichCharmConfigVo.maxLevel
            when {
                maxLevel <= 20 -> mBinding.root.setBackgroundResource(R.drawable.me_rich_charm_back_0_10)

                maxLevel <= 40 -> mBinding.root.setBackgroundResource(R.drawable.me_rich_charm_back_10_19)

                maxLevel <= 60 -> mBinding.root.setBackgroundResource(R.drawable.me_rich_charm_back_20_29)

                maxLevel <= 80 -> mBinding.root.setBackgroundResource(R.drawable.me_rich_charm_back_30_39)

                maxLevel <= 100 -> mBinding.root.setBackgroundResource(R.drawable.me_rich_charm_back_40_49)

                maxLevel <= 120 -> mBinding.root.setBackgroundResource(R.drawable.me_rich_charm_back_50_59)

                maxLevel <= 140 -> mBinding.root.setBackgroundResource(R.drawable.me_rich_charm_back_60_69)

                maxLevel <= 160 -> mBinding.root.setBackgroundResource(R.drawable.me_rich_charm_back_70_79)

                maxLevel <= 180 -> mBinding.root.setBackgroundResource(R.drawable.me_rich_charm_back_80_89)

                maxLevel <= 200 -> mBinding.root.setBackgroundResource(R.drawable.me_rich_charm_back_90_100)

                else -> mBinding.root.setBackgroundResource(R.drawable.me_rich_charm_back_90_100)
            }
        }
    }

    companion object {

        const val PAGE = "page"

        const val RICH = "rich"
        const val CHARM = "charm"
    }
}